package com.econ.powercloud.ems.service.biz;

import com.econ.powercloud.common.base.facade.enums.StatusType;
import com.econ.powercloud.common.base.facade.pojo.MsgResult;
import com.econ.powercloud.ems.common.enums.FaultDesc;
import com.econ.powercloud.ems.common.util.AesUtil;
import com.econ.powercloud.ems.dal.entity.EmsUserOperateToken;
import com.econ.powercloud.ems.service.dao.EmsUserOperateTokenDAO;
import com.econ.powercloud.ems.util.RedisCacheUtil;
import com.econ.powercloud.powernode.gateway.base.utils.CurrentUserUtil;
import com.econ.powercloud.powernode.gateway.common.constants.SysConstants;
import com.econ.powercloud.powernode.gateway.common.domain.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserOperateTokenService {
    public static final String DEFAULT_PASSWORD = "111111";

    @Autowired
    EmsUserOperateTokenDAO emsUserOperateTokenDAO;

    @Autowired
    RedisCacheUtil redisCacheUtil;

    public MsgResult verifyForPassword(String password, Long validTime){
        //获取用户信息
        UserDTO userDTO = CurrentUserUtil.get();
        EmsUserOperateToken userOperateToken = emsUserOperateTokenDAO.selectUserOperateByUserId(userDTO.getId());
        BCryptPasswordEncoder encode = new BCryptPasswordEncoder();
        //解密得到原文
        String originPassword = AesUtil.decryptAes(password);
        if(userOperateToken == null){
            if(originPassword.equals(DEFAULT_PASSWORD)){
                //证明这个账号是新增的,需要往数据库插入数据
                redisCacheUtil.addUserOperateAccessToken(userDTO.getId(),validTime);
                EmsUserOperateToken operateToken = EmsUserOperateToken.builder()
                        .userOperateId(userDTO.getId()).userName(userDTO.getName()).operatePassword(encode.encode(DEFAULT_PASSWORD))
                        .lastLogin(LocalDateTime.now()).gmtCreate(LocalDateTime.now()).gmtModify(LocalDateTime.now())
                        .build();
                emsUserOperateTokenDAO.save(operateToken);
                return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
            }
            return MsgResult.builder().status(FaultDesc.PASSWORD_ERROR.getCode()).statusText(FaultDesc.PASSWORD_ERROR.getDesc()).build();
        }
        String operatePassword = userOperateToken.getOperatePassword();
        if(encode.matches(originPassword,operatePassword)){
            redisCacheUtil.addUserOperateAccessToken(userDTO.getId(),validTime);
            emsUserOperateTokenDAO.updateLastLoginByUserId(userDTO.getId());
            return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
        }
        return MsgResult.builder().status(FaultDesc.PASSWORD_ERROR.getCode()).statusText(FaultDesc.PASSWORD_ERROR.getDesc()).data(true).build();
    }

    public MsgResult updatePasswordForUserName(String oldPassword,String newPassword){
        //解密得到原文
        String originOldPassword = AesUtil.decryptAes(oldPassword);
        String originNewPassword = AesUtil.decryptAes(newPassword);
        //获取用户信息
        UserDTO userDTO = CurrentUserUtil.get();
        String userId = userDTO.getId();
        EmsUserOperateToken userOperateToken = emsUserOperateTokenDAO.selectUserOperateByUserId(userId);
        BCryptPasswordEncoder encode = new BCryptPasswordEncoder();
        if(userOperateToken == null){
            if(DEFAULT_PASSWORD.equals(originOldPassword)){
                //证明这个账号是新增的,需要往数据库插入数据
                EmsUserOperateToken operateToken = EmsUserOperateToken.builder()
                        .userOperateId(userDTO.getId()).userName(userDTO.getName()).operatePassword(encode.encode(originNewPassword))
                        .lastLogin(LocalDateTime.now()).gmtCreate(LocalDateTime.now()).gmtModify(LocalDateTime.now())
                        .build();
                emsUserOperateTokenDAO.save(operateToken);
                return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
            }
            return MsgResult.builder().status(FaultDesc.PASSWORD_ERROR.getCode()).statusText(FaultDesc.PASSWORD_ERROR.getDesc()).build();
        }
        String operatePassword = userOperateToken.getOperatePassword();
        if(encode.matches(originOldPassword,operatePassword)){
            emsUserOperateTokenDAO.updatePassWordByUserId(encode.encode(originNewPassword),userId);
            return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
        }
        return MsgResult.builder().status(StatusType.OLD_PASSWORD_ERROR.getCode()).statusText(StatusType.OLD_PASSWORD_ERROR.getStatusText())
                .data(true).build();
    }
}
